#ifndef GAME_H
#define GAME_H

#include "Singleton.h"

#include <QDebug>
#include <QList>
#include <QMap>
#include <exception>
#include <QTimer>
#include <QObject>
#include <iostream>

class Subject;

class Observer {
  public:
    Observer(){}
    virtual ~Observer(){}
    virtual void Update(Subject* sub) = 0;
};

class Subject: public QObject{
    Q_OBJECT
  public:
    Subject(){}
    virtual ~Subject(){}
    virtual void Attach(Observer* o);
    virtual void Detach(Observer*);
    virtual void Notify();
private:
    std::list<Observer*> _observers;
};

class ClockTimer: public Subject {
    Q_OBJECT
  public:
    ClockTimer();
    virtual ~ClockTimer();

    virtual int GetHour() { return hour; }
    virtual int GetMinute() { return minute; }
    virtual int GetSecond() { return second; }

public slots:
    void Tick() {
        second++;
        if(second>=60){
           minute++; second = 0;
        }
        if(minute>=60){
           hour++; minute = 0;
        }
        if(hour>=24) hour = 0;

        qInfo()<<hour<<minute<<second;

        Notify();
    }
private:
    QTimer* _timer;
    int hour;
    int minute;
    int second;
};

class DigitalClock: public Observer {
public:
    DigitalClock(ClockTimer* c){
        _c = c;
        _c->Attach(this);
    }
    virtual ~DigitalClock(){
        _c->Detach(this);
    }
    virtual void Update(Subject *sub){
        if(_c == sub)
        {
            qInfo() << ((ClockTimer*)sub)->GetHour()
                      << ":" << ((ClockTimer*)sub)->GetMinute()
                      << ":" << ((ClockTimer*)sub)->GetSecond();
        }
    }

    ClockTimer* _c;
};


#endif // GAME_H
